package com.mxchip.livestarmobile.mobile.ui.contactdetail

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.base.getString
import com.mxchip.livestarmobile.mobile.db.AppDatabase
import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.db.entity.Contact
import com.mxchip.livestarmobile.mobile.state.SystemData
import com.mxchip.livestarmobile.mobile.ui.Call
import com.mxchip.livestarmobile.mobile.ui.GroupCall
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.*
import java.util.*

class ContactDetailViewModel(application: Application) : AndroidViewModel(application) {

    var contact: LiveData<Contact> = MutableLiveData() // private set

    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    private val appDatabase: AppDatabase = (application as Appli).database

    fun updateContact() {
        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.contactDao().update(contact.value!!)
        }
    }

    fun deleteContact() {

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.contactDao().delete(contact.value!!)
        }

    }

    fun insertCallLog() {

        val entity = CallLog(userId = userId)

        entity.number = contact.value!!.hxbNumber
        entity.type = "2"
        entity.startTime = Date()
        entity.isCaller = true

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.calllogDao().insertAll(entity)
        }
    }

    fun loadContact(contactId: Long) {
        contact = appDatabase.contactDao().loadAllById(contactId)
    }

    fun makeCall(number:String): Call {

        if(number.isNullOrBlank()) {
            throw Throwable("号码为空")
        }

        val call = when {
            number.length <= 8 -> {

                val aNumber = number.padStart(8, '0')

                if(aNumber == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(aNumber)
            }

            number.length == 11 -> {

                if(number == SystemData.userId()) {
                    throw Throwable(getString(R.string.called_local_number))
                }

                P2PSingleCall(number)
            }

            number.length == 12 -> {
                if (number.contains("#")){
                    P2PSingleCall(number)
                }else{
                    GroupCall(number)
                }
            }

            number.length == 15 -> {

                if (number.contains("#")) {
                    P2PSingleCall(number)
                } else {
                    throw Throwable(getString(R.string.called_number_error))
                }
            }

            number.length == 16 -> {
                if (number.contains("#")){
                    GroupCall(number)
                }else{
                    throw Throwable(getString(R.string.called_number_error))
                }
            }
            else -> {
                throw Throwable(getString(R.string.called_number_error))
            }
        }

        return call
    }
}